package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.HomeNewColorBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3SelectColorAdapter extends RecyclerView.Adapter<X3SelectColorAdapter.MyViewHolder> {

    private Context context;

    private X3SelectColorAdapter.OnItemClick mOnItemClick;

    private List<Boolean> selectList;

    /**
     * 这个是数据集合
     */
    private List<HomeNewColorBean> mList;

    public X3SelectColorAdapter(Context context) {
        this.context = context;
        this.mList = new ArrayList<>();
        selectList = new ArrayList<>();

    }

    public void changeColor(List<HomeNewColorBean> mList) {
        this.mList = mList;
        this.selectList.clear();
        for (int i = 0; i < mList.size(); i++) {
            selectList.add(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3SelectColorAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.x3_adapter_select_color, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (selectList.get(position)) {
            holder.v_stoke.setVisibility(View.VISIBLE);
        }else {
            holder.v_stoke.setVisibility(View.INVISIBLE);
        }

        GradientDrawable drawable = new GradientDrawable();
        //设置圆角角度
        drawable.setCornerRadius(X3ScreenUtils.dipToPx(46,context));
        //设置背景色
        String rgb = mList.get(position).getKwParam().getRgbCodeBak();
        if (!X3StringUtils.isEmpty(rgb)) {
            drawable.setColor(Color.parseColor(rgb));
        }
        holder.v_bg.setBackground(drawable);

        holder.itemView.setOnClickListener(view -> {
            for (int i = 0; i < selectList.size(); i++) {
                selectList.remove(i);
                selectList.add(i,i == position);
            }
            if (mOnItemClick != null) {
                mOnItemClick.onClicks(mList.get(position));
            }

            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private View v_stoke;
        private View v_bg;
        public MyViewHolder(View itemView) {
            super(itemView);
            v_stoke = itemView.findViewById(R.id.v_stoke);
            v_bg = itemView.findViewById(R.id.v_bg);
        }
    }

    public void setmOnItemClick(X3SelectColorAdapter.OnItemClick mOnItemClick) {
        this.mOnItemClick = mOnItemClick;
    }

    public interface OnItemClick {
        void onClicks(HomeNewColorBean colorBean);
    }
}

